package net.uhuijia.appbase.redis.impl;

import net.uhuijia.appbase.redis.IRedisDao;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Repository
public class RedisDao <K,V> implements IRedisDao<K, V> {
    @Resource
    private RedisTemplate<K, V> redisTemplate;

    @Override
    public void unwatch() {

        this.redisTemplate.unwatch();
    }

    @Override
    public boolean exec() {
        List<Object> result = this.redisTemplate.exec();
        if(result!=null&&result.size()>0)
            return true;
        else
            return false;
    }

    @Override
    public void watch(K k) {
        this.redisTemplate.watch(k);
    }

    @Override
    public void set(K k, V v) {
        this.redisTemplate.opsForValue().set(k,v);
    }

    @Override
    public V get(K k) {
        return this.redisTemplate.opsForValue().get(k);
    }

    @Override
    public void delete(K k) {
        this.redisTemplate.delete(k);
    }

    @Override
    public void set(K k, V v, long timeout) {
        this.redisTemplate.opsForValue().set(k, v, timeout, TimeUnit.SECONDS);
    }

    @Override
    public long getExpire(K k) {
        return this.redisTemplate.getExpire(k);
    }

    @Override
    public void setExpire(K k, long timeout) {
        this.redisTemplate.expire(k, timeout, TimeUnit.SECONDS);
    }

    @Override
    public void leftPush(K k, V v) {
        this.redisTemplate.opsForList().leftPush(k, v);
    }

    @Override
    public void leftPush(K k, Collection<V> vs) {
        this.redisTemplate.opsForList().leftPushAll(k, vs);
    }

    @Override
    public V rightPop(K k) {
        return this.redisTemplate.opsForList().rightPop(k);
    }

    @Override
    public long listSize(K k) {
        return this.redisTemplate.opsForList().size(k);
    }
}
